package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import com.netflix.discovery.converters.Auto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * @package com.leyou.item.service
 * @description:
 * @author: 86157
 * @date 2019/6/25 15:29
 * @version: V1.0
 */
@Service
public class BrandService {
    @Autowired
    private BrandMapper brandMapper;

    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows, String key, String sortBy, Boolean desc) {
        //分页
        PageHelper.startPage(page, rows);
//过滤条件
        Example example = new Example(Brand.class);
        if (StringUtils.isNoneBlank(key)) {
            example.createCriteria().orLike("name", "%" + key + "%").orEqualTo("letter", key.toUpperCase());
        }

        //排序
        if (StringUtils.isNoneBlank(sortBy)) {
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }

        List<Brand> brands = brandMapper.selectByExample(example);

        //解析结果集
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
//解析分页结果
        PageInfo<Brand> info = new PageInfo<>(brands);
        //获取总条数
        long total = info.getTotal();

        //转为BrandDTO
        List<BrandDTO> brandDTOS = BeanHelper.copyWithCollection(brands, BrandDTO.class);

        return new PageResult<>(total, brandDTOS);


    }


    /**
     * 新增品牌
     *
     * @param brandDTO
     * @param cids
     */
    @Transactional
    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
//将BrandDTO转变为Brand
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        brand.setId(null);

        int i = brandMapper.insertSelective(brand);
        if (i != 1) {
            //新增失败,抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }

        //新增品牌分类中间表
        i = brandMapper.insertBrandCategory(brand.getId(), cids);

        if (i != cids.size()) {
            //新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }

    }

    @Transactional
    public void updateBrand(BrandDTO brandDTO, List<Long> cids) {

        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);

        int update = brandMapper.updateByPrimaryKey(brand);

        if (update != 1) {
            //修改失败，抛出异常
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }

        //删除中间表
        brandMapper.deleteBrandCategory(brand.getId());


        //从新更新中间表
        int i = brandMapper.insertBrandCategory(brand.getId(), cids);
        if (i != cids.size()) {
            //新增失败
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }


    /**
     * 根据品牌id查询品牌
     */
    public BrandDTO queryBrand(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);

    }

    public List<BrandDTO> queryBrandByCategory(Long id) {

        List<Brand> list = brandMapper.queryBrandByCategory(id);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }
}